#!/bin/bash

input_env_value=${1}
# 地方政府服务平台
function install_government-platform() {
  if [ "$input_env_value" == "dev" ];then

    # 先用docker-compose构建环境
    echo "构建环境..."
    docker-compose -f ./develop/docker-compose.yml up -d government-platform

    # 切换源
    docker exec -it dev_government-platform bash -c "sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories"

    # 休息一秒钟
    sleep 1
    echo "部署前端..."
    git clone https://gitee.com/wang-haifeng2021/dandongdifangqianduan.git /opt/wwwroot/www/government-platform-vue
    docker exec -it dev_government-platform bash -c "cd /www/government-platform-vue && apk add yarn && yarn install && git config core.filemode false"

    echo "部署后端..."
    git clone https://gitee.com/wang-haifeng2021/dandongdifanghoutai-toll-collection-system.git /opt/wwwroot/www/government-platform
    docker exec -it dev_government-platform bash -c "cd /www/government-platform && composer install && php bin/hyperf.php mine:install && php bin/hyperf.php mine:install && git config core.filemode false"

    # 重启容器
    docker restart dev_government-platform
    # 设置读写权限
    chmod -R 777 /opt/wwwroot/www/government-platform
    chmod -R 777 /opt/wwwroot/www/government-platform-vue

    current_ip=$(hostname -I | cut -d ' ' -f1)
    echo '----------------部署成功----------------'
    echo "后端：$current_ip:9301"
    echo "前端：$current_ip:2388"
  else
    echo -e "\033[31m $input_env_value 环境，还不支持此项目 \e[0m"
  fi
}

# 安装 mineadmin 项目
function install_mineadmin() {
  if [ "$input_env_value" == "dev" ];then

    # 先用docker-compose构建环境
    echo "构建环境..."
    docker-compose -f ./develop/docker-compose.yml up -d mineadmin

    # 切换源
    docker exec -it dev_mineadmin bash -c "sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories"

    # 休息一秒钟
    sleep 1
    echo "部署前端..."
    git clone https://gitee.com/mineadmin/mineadmin-vue.git /opt/wwwroot/www/mineadmin-vue
    docker exec -it dev_mineadmin bash -c "cd /www/mineadmin-vue && apk add yarn && yarn install"


    echo "部署后端..."
    git clone https://gitee.com/mineadmin/mineadmin.git /opt/wwwroot/www/mineadmin
    docker exec -it dev_mineadmin bash -c "cd /www/mineadmin && composer install && php bin/hyperf.php mine:install && php bin/hyperf.php mine:install"

    # 重启容器
    docker restart dev_mineadmin
    # 设置读写权限
    chmod -R 777 /opt/wwwroot/www/mineadmin
    chmod -R 777 /opt/wwwroot/www/mineadmin-vue

    current_ip=$(hostname -I | cut -d ' ' -f1)
    echo '----------------部署成功----------------'
    echo "后端：$current_ip:9101"
    echo "前端：$current_ip:2188"
  else
    echo -e "\033[31m $input_env_value 环境，还不支持此项目 \e[0m"
  fi
}




# 安装 东兴二级市场 项目：install_dongxing02 环境变量
function install_dongxing02() {
  if [ "$input_env_value" == "dev" ];then

    # 先用docker-compose构建环境
    echo "构建环境..."
    docker-compose -f ./develop/docker-compose.yml up -d fastadmin

    # 休息一秒钟
    sleep 1

    # 拉取项目
    git clone https://gitee.com/wang-haifeng2021/dongxing-secondary-market.git /opt/wwwroot/www/dongxing02

    cp develop/dongxing02.com.conf /opt/wwwroot/nginx/conf.d/

    # 设置读写权限
    chmod -R 777 /opt/wwwroot/www/dongxing02
    # git 忽略权限文件
    cd /opt/wwwroot/www/dongxing02 && git config core.filemode false

    # 配置虚拟域名
    updateHost 127.0.0.1 dongxing02.com

    # 重启容器
    docker restart dev_fastadmin
    docker restart dev_nginx
    echo '----------------部署成功(请配置项目数据库连接信息)----------------'
    echo "总后台：dongxing02.com/vLnqClrwNM.php"
    echo "商家后台：dongxing02.com/index/wanlshop.console"
  else
    echo -e "\033[31m $input_env_value 环境，还不支持此项目 \e[0m"
  fi
}
# 安装 unichat 项目
function install_unichat() {
  if [ "$input_env_value" == "prod" ];then

    # 先用docker-compose构建环境
    echo "构建环境..."
    docker-compose -f ./production/docker-compose.yml up -d unichat

    # 切换源
    docker exec -it prod_unichat bash -c "sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories"

    # 休息一秒钟
    sleep 1
    echo "部署项目..."
    # 部署后端
    docker exec -it prod_unichat bash -c "git clone https://gitee.com/gogls/unichat.git /www/unichat && cd /www/unichat && composer install && php bin/hyperf.php mine:install && php bin/hyperf.php mine:install"

    # 配置nginx
    cp production/ai.goooo.love.conf /opt/wwwroot/nginx/conf.d/

    # 重启容器
    docker restart prod_unichat
    docker restart prod_nginx

    current_ip=$(hostname -I | cut -d ' ' -f1)
    echo '----------------部署成功----------------'
    echo "后端：$current_ip:9101"
    echo "前端：src/production/ai.goooo.love.conf 里配置的域名，文件名可任意更改。"
  else

    # 先用docker-compose构建环境
    echo "构建环境..."
    docker-compose -f ./develop/docker-compose.yml up -d unichat

    # 休息一秒钟
    sleep 1
    echo "部署admin..."
    # 部署前端
    docker exec -it dev_unichat bash -c "git clone https://gitee.com/gogls/unichat-admin.git /www/unichat-admin && cd /www/unichat-admin && apk add yarn && yarn install"
    echo "部署主程序..."
    # 部署后端
    docker exec -it dev_unichat bash -c "git clone https://gitee.com/gogls/unichat.git /www/unichat && cd /www/unichat && composer install && php bin/hyperf.php mine:install && php bin/hyperf.php mine:install"

    # 重启容器
    docker restart dev_unichat
    # 设置读写权限
    chmod -R 777 /opt/wwwroot/www/unichat
    chmod -R 777 /opt/wwwroot/www/unichat-admin

    current_ip=$(hostname -I | cut -d ' ' -f1)
    echo '----------------部署成功----------------'
    echo "后端：$current_ip:9201"
    echo "前端：$current_ip:2288"
  fi
}
